/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaz;

import dominio.Asignatura;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import negocio.AsignaturaNegocio;
import utilidades.JFondo;
import utilidades.Validaciones;
//import utilidades.PopupEspecialidades;

/**
 *
 * @author Richard
 */
public class JIF_asignatura extends javax.swing.JInternalFrame {

    /**
     * Creates new form JIF_asignatura
     */
    private JButton JB_ingresarAsignatura;
    private JButton JB_actualizarAsignatura;
    private JButton JB_borrarAsignatura;
    private AsignaturaNegocio asignaturaNegocio;
    private final int INSERCION = 1;
    private final int MODIFICACION = 2;
    private int opcion;
//    private PopupEspecialidades popupEspecialidades;

    public JIF_asignatura() throws SQLException {
        initComponents();
        llenarTablas();
        cargarBotonesDeBarra();
        cargarEventosDeCuadrosDeBusqueda();
        ocultarPanelSubareas();
        eventoComboTipoAsignatura();
        habilitarEspacios(false);
        JP_asignaturaGeneral.setBorder(new JFondo("../imagenes/fondo_2.jpg"));
    }

    private void llenarTablas() throws SQLException {
        asignaturaNegocio = new AsignaturaNegocio();
        JT_asignaturasAcademicas.setModel(asignaturaNegocio.getAsignaturasAcademicas(""));
        JT_asignaturasTecnicas.setModel(asignaturaNegocio.getAsignaturasTecnicas(""));
        if (JT_asignaturasTecnicas.getRowCount() > 0) {
            JT_asignaturasTecnicas.getColumn("Nivel").setPreferredWidth(0);
            JT_asignaturasTecnicas.getColumn("Nivel").setMinWidth(0);
            JT_asignaturasTecnicas.getColumn("Nivel").setWidth(0);
            JT_asignaturasTecnicas.getColumn("Nivel").setMaxWidth(0);
        }
    }//Fin llenarTablas()

    private void cargarBotonesDeBarra() {
        JB_ingresarAsignatura = new JButton("Nueva");
        JB_actualizarAsignatura = new JButton("Actualizar");
        JB_borrarAsignatura = new JButton("Borrar");
        JMB_barraAsignaturas.add(JB_ingresarAsignatura);
        JB_ingresarAsignatura.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                opcion = INSERCION;
                quitarSeleccionTecnicas();
                quitarSeleccionAcademicas();
                limiparEspacios();
                habilitarEspacios(true);
                JP_especialidadesLigadasSubarea.setVisible(false);
            }
        });
        /*JB_ingresarAsignatura.addActionListener((new ActionEvent ae) {
         opcion = INSERCION;
         quitarSeleccionTecnicas();
         quitarSeleccionAcademicas();
         limiparEspacios();
         habilitarEspacios(true);
         JP_especialidadesLigadasSubarea.setVisible(false);
         });*/
        JMB_barraAsignaturas.add(JB_actualizarAsignatura);

        JB_actualizarAsignatura.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int indiceTecnicas = JT_asignaturasTecnicas.getSelectedRow();
                int indiceAcademicas = JT_asignaturasAcademicas.getSelectedRow();
                if (indiceTecnicas > -1 || indiceAcademicas > -1) {
                    opcion = MODIFICACION;
                    quitarSeleccionTecnicas();
                    quitarSeleccionAcademicas();
                    habilitarEspacios(true);
                    JTF_siglaAsignatura.setEditable(false);
                } else {
                    JOptionPane.showMessageDialog(null, "Debe seleccionar la asignatura a modificar");
                }
            }
        });
        /*JB_actualizarAsignatura.addActionListener((ActionEvent ae) -> {
         int indiceTecnicas = JT_asignaturasTecnicas.getSelectedRow();
         int indiceAcademicas = JT_asignaturasAcademicas.getSelectedRow();
         if (indiceTecnicas > -1 || indiceAcademicas > -1) {
         opcion = MODIFICACION;
         quitarSeleccionTecnicas();
         quitarSeleccionAcademicas();
         habilitarEspacios(true);
         JTF_siglaAsignatura.setEditable(false);
         } else {
         JOptionPane.showMessageDialog(null, "Debe seleccionar la asignatura a modificar");
         }

         });*/
        JMB_barraAsignaturas.add(JB_borrarAsignatura);
        JB_borrarAsignatura.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                int indiceTecnicas = JT_asignaturasTecnicas.getSelectedRow();
                int indiceAcademicas = JT_asignaturasAcademicas.getSelectedRow();
                if (indiceTecnicas > -1 || indiceAcademicas > -1) {
                    borrarAsignatura();
                } else {
                    JOptionPane.showMessageDialog(null, "Debe seleccionar la asignatura a borrar");
                }

            }
        });
        /*JB_borrarAsignatura.addActionListener((ActionEvent ae) -> {
         borrarAsignatura();
         });*/

    }// Fin cargarBotonesDeBarra()

    private void cargarEventosDeCuadrosDeBusqueda() {
        JTF_buscarAsignaturaAcademica.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent de) {
                try {
                    JT_asignaturasAcademicas.setModel(asignaturaNegocio.getAsignaturasAcademicas(JTF_buscarAsignaturaAcademica.getText()));
                } catch (SQLException ex) {
                }
            }

            @Override
            public void removeUpdate(DocumentEvent de) {
                try {
                    JT_asignaturasAcademicas.setModel(asignaturaNegocio.getAsignaturasAcademicas(JTF_buscarAsignaturaAcademica.getText()));
                } catch (SQLException ex) {
                }
            }

            @Override
            public void changedUpdate(DocumentEvent de) {
                try {
                    JT_asignaturasAcademicas.setModel(asignaturaNegocio.getAsignaturasAcademicas(JTF_buscarAsignaturaAcademica.getText()));
                } catch (SQLException ex) {
                }
            }
        });

        JTF_buscarAsignaturaTecnica.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent de) {
                try {
                    JT_asignaturasTecnicas.setModel(asignaturaNegocio.getAsignaturasTecnicas(JTF_buscarAsignaturaTecnica.getText()));
                } catch (SQLException ex) {
                }
            }

            @Override
            public void removeUpdate(DocumentEvent de) {
                try {
                    JT_asignaturasTecnicas.setModel(asignaturaNegocio.getAsignaturasTecnicas(JTF_buscarAsignaturaTecnica.getText()));
                } catch (SQLException ex) {
                }
            }

            @Override
            public void changedUpdate(DocumentEvent de) {
                try {
                    JT_asignaturasTecnicas.setModel(asignaturaNegocio.getAsignaturasTecnicas(JTF_buscarAsignaturaTecnica.getText()));
                } catch (SQLException ex) {
                }
            }
        });

        JT_asignaturasTecnicas.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                try {
                    actualizarCamposAsignaturaTecnica();
                } catch (SQLException ex) {
                }
            }
        });
        /*JT_asignaturasTecnicas.getSelectionModel().addListSelectionListener((ListSelectionEvent lse) -> {
         try {
         actualizarCamposAsignaturaTecnica();
         } catch (SQLException ex) {
         }
         })*/

        JT_asignaturasAcademicas.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                actualizarCamposAsignaturaAcademica();
            }
        });
        /*JT_asignaturasAcademicas.getSelectionModel().addListSelectionListener((ListSelectionEvent lse) -> {
         actualizarCamposAsignaturaAcademica();
         });*/

        JT_asignaturasAcademicas.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                quitarSeleccionTecnicas();
            }
        });
        JT_asignaturasTecnicas.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                quitarSeleccionAcademicas();
            }
        });
    }

//    private void crearPopupAsignatura() throws SQLException {
//        popupEspecialidades = new PopupEspecialidades();
//        popupEspecialidades.setVisible(true);
//    }
    private void quitarSeleccionAcademicas() {
        int row = JT_asignaturasAcademicas.getSelectedRow();
        if (row >= -1) {
            JT_asignaturasAcademicas.clearSelection();
        }
    }

    private void quitarSeleccionTecnicas() {
        int row = JT_asignaturasTecnicas.getSelectedRow();
        if (row >= -1) {
            JT_asignaturasTecnicas.clearSelection();
        }
    }

    private void actualizarCamposAsignaturaAcademica() {
        habilitarEspacios(false);
        ocultarPanelSubareas();
        int indice = JT_asignaturasAcademicas.getSelectedRow();
        if (indice > -1) {
            JTF_siglaAsignatura.setText(JT_asignaturasAcademicas.getValueAt(indice, 0).toString());
            JTF_nombreAsignatura.setText(JT_asignaturasAcademicas.getValueAt(JT_asignaturasAcademicas.getSelectedRow(), 1).toString());
            JSP_horasSemanales.setValue(Float.parseFloat(JT_asignaturasAcademicas.getValueAt(JT_asignaturasAcademicas.getSelectedRow(), 2).toString()));
            JCB_tipoAsignatura.setSelectedItem("Académica");
        }
    }

    private void actualizarCamposAsignaturaTecnica() throws SQLException {
        habilitarEspacios(false);
        int indice = JT_asignaturasTecnicas.getSelectedRow();
        if (indice > -1) {
            JP_especialidadesLigadasSubarea.setVisible(true);
            JTF_siglaAsignatura.setText(JT_asignaturasTecnicas.getValueAt(indice, 0).toString());
            JTF_nombreAsignatura.setText(JT_asignaturasTecnicas.getValueAt(JT_asignaturasTecnicas.getSelectedRow(), 1).toString());
            JSP_horasSemanales.setValue(Float.parseFloat(JT_asignaturasTecnicas.getValueAt(JT_asignaturasTecnicas.getSelectedRow(), 2).toString()));
            JSP_nivel.setValue(Integer.parseInt(JT_asignaturasTecnicas.getValueAt(JT_asignaturasTecnicas.getSelectedRow(), 3).toString()));
            JCB_tipoAsignatura.setSelectedItem("Técnica");
//        llenarTablaEspecialidades();
        }
    }

    private void eventoComboTipoAsignatura() {

        JCB_tipoAsignatura.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getItem() == "Técnica") {
                    JP_especialidadesLigadasSubarea.setVisible(true);
                } else {
                    JP_especialidadesLigadasSubarea.setVisible(false);
                }
            }
        });

    }
    /*});
     JCB_tipoAsignatura.addItemListener((ItemEvent ie) -> {
     if (ie.getItem() == "Técnica") {
     JP_especialidadesLigadasSubarea.setVisible(true);
     } else {
     JP_especialidadesLigadasSubarea.setVisible(false);
     }
     });
     }
     */
//    private void llenarTablaEspecialidades() throws SQLException {
//        asignaturaNegocio = new AsignaturaNegocio();
//        String siglaSeleccionada = JT_asignaturasTecnicas.getValueAt(JT_asignaturasTecnicas.getSelectedRow(), 0).toString();
//        JT_especialidadesDeSubarea.setModel(asignaturaNegocio.getEspecialidadesDeSubarea(siglaSeleccionada));
//        if (JT_especialidadesDeSubarea.getRowCount() > 0) {
//            JT_especialidadesDeSubarea.getColumn("idEspecialidad").setPreferredWidth(0);
//            JT_especialidadesDeSubarea.getColumn("idEspecialidad").setMinWidth(0);
//            JT_especialidadesDeSubarea.getColumn("idEspecialidad").setWidth(0);
//            JT_especialidadesDeSubarea.getColumn("idEspecialidad").setMaxWidth(0); 
//        }
//    }//Fin llenarTablas()

    private void ocultarPanelSubareas() {
//        JSP_nivel.setValue(7);
//        try {
//	            DefaultTableModel modelo=(DefaultTableModel) JT_especialidadesDeSubarea.getModel();
//	            int filas=JT_especialidadesDeSubarea.getRowCount();
//	            for (int i = 0;filas>i; i++) {
//	                modelo.removeRow(0);
//	            }
//	        } catch (Exception e) {}
        JP_especialidadesLigadasSubarea.setVisible(false);
    }

    private void habilitarEspacios(boolean valor) {
        JCB_tipoAsignatura.setEnabled(valor);
        JTF_siglaAsignatura.setEditable(valor);
        JTF_nombreAsignatura.setEditable(valor);
        JSP_horasSemanales.setEnabled(valor);
        JSP_nivel.setEnabled(valor);
        JP_botoneraAceptCancel.setVisible(valor);
    }

    private void limiparEspacios() {
        JCB_tipoAsignatura.setSelectedIndex(0);
        JTF_siglaAsignatura.setText("");
        JTF_nombreAsignatura.setText("");
        JSP_horasSemanales.setValue(1);
        JSP_nivel.setValue(10);
    }

    private void borrarAsignatura() {
        String sigla = JTF_siglaAsignatura.getText();
        try {
            asignaturaNegocio.borrarAsignatura(sigla);
            limiparEspacios();
            llenarTablas();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "No se pudo borrar la asignatura");
        }
    }

    private void insertarAsignatura() throws SQLException {
        String sigla = JTF_siglaAsignatura.getText();
        String nombre = JTF_nombreAsignatura.getText();
        float horasSemanales = Float.parseFloat(JSP_horasSemanales.getValue().toString());
        int nivel = Integer.parseInt(JSP_nivel.getValue().toString());
        boolean esTecnica = false;
        if (JCB_tipoAsignatura.getSelectedItem() == "Técnica") {
            esTecnica = true;
        }
        if (Validaciones.cadenasStringValidas(sigla, nombre)) {
            Asignatura asignatura = new Asignatura();
            asignatura.setSigla(sigla);
            asignatura.setNombre(nombre);
            asignatura.setHorasSemanales(horasSemanales);
            asignatura.setEsTecnica(esTecnica);
            asignatura.getSubarea().setNivel(nivel);
            asignaturaNegocio.ingresarAsignatura(asignatura);
            limiparEspacios();
            habilitarEspacios(false);
            llenarTablas();
        } else {
            JOptionPane.showMessageDialog(null, "Algunos datos no se ingresaron correctamente");
        }

    }

    private void actualizarAsignatura() throws SQLException {
        String sigla = JTF_siglaAsignatura.getText();
        String nombre = JTF_nombreAsignatura.getText();
        float horasSemanales = Float.parseFloat(JSP_horasSemanales.getValue().toString());
        int nivel = Integer.parseInt(JSP_nivel.getValue().toString());
        boolean esTecnica = false;
        if (JCB_tipoAsignatura.getSelectedItem() == "Técnica") {
            esTecnica = true;
        }
        if (Validaciones.cadenasStringValidas(sigla, nombre)) {
            Asignatura asignatura = new Asignatura();
            asignatura.setSigla(sigla);
            asignatura.setNombre(nombre);
            asignatura.setHorasSemanales(horasSemanales);
            asignatura.setEsTecnica(esTecnica);
            asignatura.getSubarea().setNivel(nivel);
            asignaturaNegocio.actualizarAsignatura(asignatura);
            limiparEspacios();
            habilitarEspacios(false);
            llenarTablas();
        } else {
            JOptionPane.showMessageDialog(null, "Algunos datos no se ingresaron correctamente");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        JP_asignaturaGeneral = new javax.swing.JPanel();
        JP_asignaturasAcademicas = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        JT_asignaturasAcademicas = new javax.swing.JTable();
        JL_asignaturasAcademicas = new javax.swing.JLabel();
        JL_buscarAignaturaAcademica = new javax.swing.JLabel();
        JTF_buscarAsignaturaAcademica = new javax.swing.JTextField();
        JP_asignaturasTecnicas = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        JT_asignaturasTecnicas = new javax.swing.JTable();
        JL_asignaturasTecnicas = new javax.swing.JLabel();
        JL_buscarAignaturaTecnica = new javax.swing.JLabel();
        JTF_buscarAsignaturaTecnica = new javax.swing.JTextField();
        JP_datosGeneralesAsignatura = new javax.swing.JPanel();
        JP_especialidadesLigadasSubarea = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        JSP_nivel = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        JCB_tipoAsignatura = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        JTF_siglaAsignatura = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        JTF_nombreAsignatura = new javax.swing.JTextField();
        JSP_horasSemanales = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        JP_botoneraAceptCancel = new javax.swing.JPanel();
        JB_aceptar = new javax.swing.JButton();
        JB_cancelar = new javax.swing.JButton();
        JMB_barraAsignaturas = new javax.swing.JMenuBar();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Mantenimiento de asignatura");
        setMaximumSize(new java.awt.Dimension(1000, 550));
        setMinimumSize(new java.awt.Dimension(100, 60));
        setPreferredSize(new java.awt.Dimension(1000, 550));
        getContentPane().setLayout(null);

        JP_asignaturaGeneral.setPreferredSize(new java.awt.Dimension(1000, 520));

        JP_asignaturasAcademicas.setBackground(new java.awt.Color(255, 255, 255));
        JP_asignaturasAcademicas.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        JT_asignaturasAcademicas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        JT_asignaturasAcademicas.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        JT_asignaturasAcademicas.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(JT_asignaturasAcademicas);

        JL_asignaturasAcademicas.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        JL_asignaturasAcademicas.setText("Asignaturas academicas");

        JL_buscarAignaturaAcademica.setText("Buscar:");

        javax.swing.GroupLayout JP_asignaturasAcademicasLayout = new javax.swing.GroupLayout(JP_asignaturasAcademicas);
        JP_asignaturasAcademicas.setLayout(JP_asignaturasAcademicasLayout);
        JP_asignaturasAcademicasLayout.setHorizontalGroup(
            JP_asignaturasAcademicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_asignaturasAcademicasLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(JP_asignaturasAcademicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(JP_asignaturasAcademicasLayout.createSequentialGroup()
                        .addComponent(JL_asignaturasAcademicas)
                        .addGap(79, 79, 79)
                        .addComponent(JL_buscarAignaturaAcademica)
                        .addGap(18, 18, 18)
                        .addComponent(JTF_buscarAsignaturaAcademica, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        JP_asignaturasAcademicasLayout.setVerticalGroup(
            JP_asignaturasAcademicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_asignaturasAcademicasLayout.createSequentialGroup()
                .addGroup(JP_asignaturasAcademicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(JP_asignaturasAcademicasLayout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(JL_asignaturasAcademicas)
                        .addGap(22, 22, 22))
                    .addGroup(JP_asignaturasAcademicasLayout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(JP_asignaturasAcademicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(JL_buscarAignaturaAcademica)
                            .addComponent(JTF_buscarAsignaturaAcademica, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38))
        );

        JP_asignaturasTecnicas.setBackground(new java.awt.Color(255, 255, 255));
        JP_asignaturasTecnicas.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        JT_asignaturasTecnicas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        JT_asignaturasTecnicas.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane3.setViewportView(JT_asignaturasTecnicas);

        JL_asignaturasTecnicas.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        JL_asignaturasTecnicas.setText("Asignaturas técnicas");

        JL_buscarAignaturaTecnica.setText("Buscar:");

        javax.swing.GroupLayout JP_asignaturasTecnicasLayout = new javax.swing.GroupLayout(JP_asignaturasTecnicas);
        JP_asignaturasTecnicas.setLayout(JP_asignaturasTecnicasLayout);
        JP_asignaturasTecnicasLayout.setHorizontalGroup(
            JP_asignaturasTecnicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_asignaturasTecnicasLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(JP_asignaturasTecnicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(JP_asignaturasTecnicasLayout.createSequentialGroup()
                        .addComponent(JL_asignaturasTecnicas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 100, Short.MAX_VALUE)
                        .addComponent(JL_buscarAignaturaTecnica)
                        .addGap(18, 18, 18)
                        .addComponent(JTF_buscarAsignaturaTecnica, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        JP_asignaturasTecnicasLayout.setVerticalGroup(
            JP_asignaturasTecnicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_asignaturasTecnicasLayout.createSequentialGroup()
                .addGroup(JP_asignaturasTecnicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(JP_asignaturasTecnicasLayout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(JL_asignaturasTecnicas)
                        .addGap(22, 22, 22))
                    .addGroup(JP_asignaturasTecnicasLayout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(JP_asignaturasTecnicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(JL_buscarAignaturaTecnica)
                            .addComponent(JTF_buscarAsignaturaTecnica, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38))
        );

        JP_datosGeneralesAsignatura.setBackground(new java.awt.Color(255, 255, 255));
        JP_datosGeneralesAsignatura.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        JP_datosGeneralesAsignatura.setPreferredSize(new java.awt.Dimension(502, 410));

        JP_especialidadesLigadasSubarea.setBackground(new java.awt.Color(255, 255, 255));

        jLabel6.setText("Nivel: ");

        JSP_nivel.setModel(new javax.swing.SpinnerNumberModel(10, 10, 12, 1));

        javax.swing.GroupLayout JP_especialidadesLigadasSubareaLayout = new javax.swing.GroupLayout(JP_especialidadesLigadasSubarea);
        JP_especialidadesLigadasSubarea.setLayout(JP_especialidadesLigadasSubareaLayout);
        JP_especialidadesLigadasSubareaLayout.setHorizontalGroup(
            JP_especialidadesLigadasSubareaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_especialidadesLigadasSubareaLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel6)
                .addGap(88, 88, 88)
                .addComponent(JSP_nivel, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(182, Short.MAX_VALUE))
        );
        JP_especialidadesLigadasSubareaLayout.setVerticalGroup(
            JP_especialidadesLigadasSubareaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_especialidadesLigadasSubareaLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(JP_especialidadesLigadasSubareaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(JSP_nivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(183, 183, 183))
        );

        jLabel4.setText("Tipo de asignatura:");

        JCB_tipoAsignatura.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Académica", "Técnica" }));

        jLabel1.setText("Sigla: ");

        jLabel2.setText("Nombre:");

        JSP_horasSemanales.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(1.0f), Float.valueOf(1.0f), Float.valueOf(24.0f), Float.valueOf(0.1f)));

        jLabel3.setText("Horas semanales:");

        JP_botoneraAceptCancel.setBackground(new java.awt.Color(255, 255, 255));

        JB_aceptar.setText("Aceptar");
        JB_aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_aceptarActionPerformed(evt);
            }
        });

        JB_cancelar.setText("Cancelar");
        JB_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_cancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout JP_botoneraAceptCancelLayout = new javax.swing.GroupLayout(JP_botoneraAceptCancel);
        JP_botoneraAceptCancel.setLayout(JP_botoneraAceptCancelLayout);
        JP_botoneraAceptCancelLayout.setHorizontalGroup(
            JP_botoneraAceptCancelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_botoneraAceptCancelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(JB_cancelar)
                .addGap(18, 18, 18)
                .addComponent(JB_aceptar)
                .addContainerGap())
        );
        JP_botoneraAceptCancelLayout.setVerticalGroup(
            JP_botoneraAceptCancelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_botoneraAceptCancelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(JP_botoneraAceptCancelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JB_aceptar)
                    .addComponent(JB_cancelar))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout JP_datosGeneralesAsignaturaLayout = new javax.swing.GroupLayout(JP_datosGeneralesAsignatura);
        JP_datosGeneralesAsignatura.setLayout(JP_datosGeneralesAsignaturaLayout);
        JP_datosGeneralesAsignaturaLayout.setHorizontalGroup(
            JP_datosGeneralesAsignaturaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_datosGeneralesAsignaturaLayout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(JP_datosGeneralesAsignaturaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(JP_datosGeneralesAsignaturaLayout.createSequentialGroup()
                        .addGroup(JP_datosGeneralesAsignaturaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(72, 72, 72)
                        .addGroup(JP_datosGeneralesAsignaturaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(JTF_nombreAsignatura)
                            .addComponent(JTF_siglaAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(JP_datosGeneralesAsignaturaLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(JSP_horasSemanales, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(JP_datosGeneralesAsignaturaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(JP_especialidadesLigadasSubarea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, JP_datosGeneralesAsignaturaLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(JP_datosGeneralesAsignaturaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, JP_datosGeneralesAsignaturaLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(JCB_tipoAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, JP_datosGeneralesAsignaturaLayout.createSequentialGroup()
                        .addComponent(JP_botoneraAceptCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(21, 21, 21))))
        );
        JP_datosGeneralesAsignaturaLayout.setVerticalGroup(
            JP_datosGeneralesAsignaturaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_datosGeneralesAsignaturaLayout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(JP_datosGeneralesAsignaturaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(JCB_tipoAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(67, 67, 67)
                .addGroup(JP_datosGeneralesAsignaturaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(JTF_siglaAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(JP_datosGeneralesAsignaturaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(JTF_nombreAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(JP_datosGeneralesAsignaturaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(JSP_horasSemanales, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(JP_especialidadesLigadasSubarea, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
                .addComponent(JP_botoneraAceptCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout JP_asignaturaGeneralLayout = new javax.swing.GroupLayout(JP_asignaturaGeneral);
        JP_asignaturaGeneral.setLayout(JP_asignaturaGeneralLayout);
        JP_asignaturaGeneralLayout.setHorizontalGroup(
            JP_asignaturaGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_asignaturaGeneralLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(JP_asignaturaGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(JP_asignaturasAcademicas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(JP_asignaturasTecnicas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addComponent(JP_datosGeneralesAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        JP_asignaturaGeneralLayout.setVerticalGroup(
            JP_asignaturaGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_asignaturaGeneralLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(JP_asignaturaGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(JP_asignaturaGeneralLayout.createSequentialGroup()
                        .addComponent(JP_asignaturasAcademicas, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(JP_asignaturasTecnicas, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(JP_datosGeneralesAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(JP_asignaturaGeneral);
        JP_asignaturaGeneral.setBounds(0, 0, 1000, 490);
        setJMenuBar(JMB_barraAsignaturas);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void JB_aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_aceptarActionPerformed
        if (opcion == INSERCION) {
            try {
                insertarAsignatura();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "La sigla ingresada ya existe");
            }
        } else {
            try {
                actualizarAsignatura();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, ex);
            }
        }
    }//GEN-LAST:event_JB_aceptarActionPerformed

    private void JB_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_cancelarActionPerformed
        try {
            limiparEspacios();
            habilitarEspacios(false);
            llenarTablas();
        } catch (SQLException ex) {
        }
    }//GEN-LAST:event_JB_cancelarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JB_aceptar;
    private javax.swing.JButton JB_cancelar;
    private javax.swing.JComboBox JCB_tipoAsignatura;
    private javax.swing.JLabel JL_asignaturasAcademicas;
    private javax.swing.JLabel JL_asignaturasTecnicas;
    private javax.swing.JLabel JL_buscarAignaturaAcademica;
    private javax.swing.JLabel JL_buscarAignaturaTecnica;
    private javax.swing.JMenuBar JMB_barraAsignaturas;
    private javax.swing.JPanel JP_asignaturaGeneral;
    private javax.swing.JPanel JP_asignaturasAcademicas;
    private javax.swing.JPanel JP_asignaturasTecnicas;
    private javax.swing.JPanel JP_botoneraAceptCancel;
    private javax.swing.JPanel JP_datosGeneralesAsignatura;
    private javax.swing.JPanel JP_especialidadesLigadasSubarea;
    private javax.swing.JSpinner JSP_horasSemanales;
    private javax.swing.JSpinner JSP_nivel;
    private javax.swing.JTextField JTF_buscarAsignaturaAcademica;
    private javax.swing.JTextField JTF_buscarAsignaturaTecnica;
    private javax.swing.JTextField JTF_nombreAsignatura;
    private javax.swing.JTextField JTF_siglaAsignatura;
    private javax.swing.JTable JT_asignaturasAcademicas;
    private javax.swing.JTable JT_asignaturasTecnicas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables
}
